package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SchoolContacts;
import cn.wolfcode.crm.query.SchoolContactsQueryObject;
import cn.wolfcode.crm.service.ISchoolContactsService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("schoolContacts")
public class SchoolContactsController {

    @Autowired
    private ISchoolContactsService schoolContactsService;

    @RequestMapping("index")
    public String index(){
        return "schoolContacts/list";
    }

    @ResponseBody
    @RequestMapping("list")
    public PageResult index(SchoolContactsQueryObject qo){
        PageResult result = schoolContactsService.queryListByQo(qo);
        return result;
    }

    @ResponseBody
    @RequestMapping("save")
    public JsonResult save(SchoolContacts schoolContacts){
        JsonResult result = new JsonResult();
        try {
            schoolContactsService.insert(schoolContacts);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("保存出错!");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("update")
    public JsonResult update(SchoolContacts schoolContacts){
        JsonResult result = new JsonResult();
        try {
            schoolContactsService.updateByPrimaryKey(schoolContacts);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("编辑出错!");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("delete")
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            if (id!=null){
                schoolContactsService.deleteByPrimaryKey(id);
            }
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("编辑出错!");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("contract")
    public JsonResult contract(Long id){
        JsonResult result = new JsonResult();
        try {
            schoolContactsService.contract(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("编辑出错!");
        }
        return result;
    }

    @RequestMapping("exportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception{
        //导出
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String fileName = sdf.format(new Date()) + ".xls";
        response.setContentType("application/x-excel");
        response.setCharacterEncoding("UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename="
                + fileName);
        List<SchoolContacts> list = new ArrayList<>();
        List<SchoolContacts> schoolContactss = schoolContactsService.selectAll();
        for (SchoolContacts schoolContacts : schoolContactss) {
            list.add(schoolContacts);
        }
        //1.创建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());
        //2.创建页签
        WritableSheet sheet = workbook.createSheet("员工列表", 0);
        //3.创建单元格
        String[] header = new String[]{"姓名","性别","所属学校","院系","部门","职务","电话","QQ","Email","状态"};        Label label = null;
        for (int i = 0; i < header.length; i++) {
            label = new Label(i,0,header[i]);
            //4.添加到页签中
            sheet.addCell(label);
        }
        //迭代数据
        for (int i = 1; i <list.size()+1 ; i++) {
            label = new Label(0,i,list.get(i-1).getName());
            sheet.addCell(label);
            label = new Label(1,i,list.get(i-1).getGender());
            sheet.addCell(label);
            if (list.get(i-1).getSchool()==null){
                label = new Label(2,i,"");
                sheet.addCell(label);
            }else {
                label = new Label(2,i,list.get(i-1).getSchool().getSchoolName());
                sheet.addCell(label);
            }
            label = new Label(3,i,list.get(i-1).getCollege());
            sheet.addCell(label);
            label = new Label(4,i,list.get(i-1).getDepartment());
            sheet.addCell(label);
            label = new Label(5,i,list.get(i-1).getDuty());
            sheet.addCell(label);
            label = new Label(6,i,list.get(i-1).getPhone());
            sheet.addCell(label);
            label = new Label(7,i,list.get(i-1).getQq());
            sheet.addCell(label);
            label = new Label(8,i,list.get(i-1).getEmail());
            sheet.addCell(label);
            label = new Label(9, i, list.get(i - 1).getState().toString());
            sheet.addCell(label);
        }
        //5.写出数据
        workbook.write();
        workbook.close();
    }
}
